package com.train.course.service.impl;

import com.train.course.domain.VideoCourse;
import com.train.course.domain.VideoTag;
import com.train.course.dto.QueryDTO;
import com.train.course.mapper.VideoCourseMapper;
import com.train.course.service.IVideoTagService;
import com.train.course.service.VideoCourseCategoryService;
import com.train.course.vo.QueryVideoCourseVO;
import com.train.course.vo.VideoTagVO;
import com.train.course.vo.VideoCourseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VideoCourseCategoryServiceImpl implements VideoCourseCategoryService {
    @Autowired
    VideoCourseMapper videoCourseMapper;

    @Autowired
    IVideoTagService videoTagService;

    @Override
    public QueryVideoCourseVO queryCourseByCategoryId(QueryDTO queryDTO) {

        List<VideoCourse> videoCourses = videoCourseMapper.queryCourseByCategoryId(queryDTO);

        List<VideoCourseVO> videoCourseVOList = new ArrayList<>();

        QueryVideoCourseVO queryVideoCourseVO = new QueryVideoCourseVO();

        if (videoCourses.isEmpty()){
            return queryVideoCourseVO;
        }

       for(VideoCourse videoCourse : videoCourses) {
           VideoCourseVO videoCourseVO = new VideoCourseVO();
           BeanUtils.copyProperties(videoCourse, videoCourseVO);
           // 填充标签信息
           fillCourseTags(videoCourseVO, videoCourse.getCourseId());
           videoCourseVOList.add(videoCourseVO);
       }

        if (videoCourseVOList.isEmpty()){
            return queryVideoCourseVO;
        }
       queryVideoCourseVO.setVideoCourseVOList(videoCourseVOList);
       queryVideoCourseVO.setLastVideoCourseId(videoCourses.get(videoCourses.size()-1).getCourseId());
       queryVideoCourseVO.setCreateTime(videoCourses.get(videoCourses.size()-1).getCreateTime());
       return queryVideoCourseVO;

    }

    @Override
    public QueryVideoCourseVO queryAllCourse(QueryDTO queryDTO) {
        List<VideoCourse> videoCourses = videoCourseMapper.queryAllCourse(queryDTO);

        List<VideoCourseVO> videoCourseVOList = new ArrayList<>();

        for(VideoCourse videoCourse : videoCourses) {
            VideoCourseVO videoCourseVO = new VideoCourseVO();
            BeanUtils.copyProperties(videoCourse, videoCourseVO);
            // 填充标签信息
            fillCourseTags(videoCourseVO, videoCourse.getCourseId());
            videoCourseVOList.add(videoCourseVO);
        }
        
        QueryVideoCourseVO queryVideoCourseVO = new QueryVideoCourseVO();
        if (videoCourseVOList.isEmpty()){
            return queryVideoCourseVO;
        }
        
        queryVideoCourseVO.setVideoCourseVOList(videoCourseVOList);
        queryVideoCourseVO.setLastVideoCourseId(videoCourses.get(videoCourses.size()-1).getCourseId());
        queryVideoCourseVO.setCreateTime(videoCourses.get(videoCourses.size()-1).getCreateTime());
        return queryVideoCourseVO;
    }

    @Override
    public QueryVideoCourseVO queryCourseBySort(QueryDTO queryDTO) {
        List<VideoCourse> videoCourses;
        
        // 根据sortType选择不同的查询方法
        if ("like".equals(queryDTO.getSortType())) {
            videoCourses = videoCourseMapper.queryCourseByLikeCount(queryDTO);
        } else if ("collect".equals(queryDTO.getSortType())) {
            videoCourses = videoCourseMapper.queryCourseByCollectCount(queryDTO);
        } else {
            // 默认按创建时间查询
            videoCourses = videoCourseMapper.queryAllCourse(queryDTO);
        }

        List<VideoCourseVO> videoCourseVOList = new ArrayList<>();
        for(VideoCourse videoCourse : videoCourses) {
            VideoCourseVO videoCourseVO = new VideoCourseVO();
            BeanUtils.copyProperties(videoCourse, videoCourseVO);
            // 填充标签信息
            fillCourseTags(videoCourseVO, videoCourse.getCourseId());
            videoCourseVOList.add(videoCourseVO);
        }
        
        QueryVideoCourseVO queryVideoCourseVO = new QueryVideoCourseVO();
        if (videoCourseVOList.isEmpty()){
            return queryVideoCourseVO;
        }
        
        VideoCourse lastCourse = videoCourses.get(videoCourses.size()-1);
        queryVideoCourseVO.setVideoCourseVOList(videoCourseVOList);
        queryVideoCourseVO.setLastVideoCourseId(lastCourse.getCourseId());
        queryVideoCourseVO.setCreateTime(lastCourse.getCreateTime());
        queryVideoCourseVO.setLastLikeCount(lastCourse.getLikeCount());
        System.out.println("喜欢数"+lastCourse.getLikeCount());
        queryVideoCourseVO.setLastCollectCount(lastCourse.getCollectCount());
        System.out.println("收藏数"+lastCourse.getCollectCount());
        
        return queryVideoCourseVO;
    }

    /**
     * 填充课程标签信息
     * 
     * @param videoCourseVO 课程VO
     * @param courseId 课程ID
     */
    private void fillCourseTags(VideoCourseVO videoCourseVO, Long courseId) {
        List<VideoTag> tags = videoTagService.selectTagsByVideoId(courseId, "1");
        if (tags != null && !tags.isEmpty()) {
            List<VideoTagVO> tagVOList = tags.stream().map(tag -> {
                VideoTagVO tagVO = new VideoTagVO();
                tagVO.setTagId(tag.getTagId());
                tagVO.setTagName(tag.getTagName());
                tagVO.setTagDesc(tag.getTagDesc());
                return tagVO;
            }).collect(Collectors.toList());
            videoCourseVO.setTags(tagVOList);
        }
    }
}
